package com.xyauto.theme

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.text.TextUtils
import com.arialyy.aria.core.Aria
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.tencent.mmkv.MMKV
import com.xyauto.theme.utils.AutoUtils
import com.xyauto.theme.utils.JieMainYiManager
import com.xyauto.theme.utils.SelectManager

class App : Application(){


    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var instance: Context
        private var launcherVer: String? = null
        fun findLauncherVer(): String {
            if (TextUtils.isEmpty(launcherVer)) {
                launcherVer = try {
                    val packageName = Constant.PAK_NEW_LAUNCHER.split("&&")[0]
                    instance.packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)?.let {
                        if (it.versionName.contains("_")) {
                            it.versionName.split("_")[1]
                        } else {
                            ""
                        }
                    }
                } catch (e: Exception) {
                    LogUtils.e("getDesignMetaData: ${e.message}")
                    ""
                }
            }
            return launcherVer!!
        }
    }


    init {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColorsId(R.color.transparent, R.color.dark_color) //全局设置主题颜色
            ClassicsHeader(context) //指定为经典Header，默认是 贝塞尔雷达Header
        }
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ -> //指定为经典Footer，默认是 BallPulseFooter
            ClassicsFooter(context).setDrawableSize(20f)
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        //工具类初始化
        Utils.init(this)
        //LogDebug设置
        LogUtils.getConfig().setLogSwitch(BuildConfig.DEBUG)
        //MMKV初始化
        MMKV.initialize(this)
        //屏幕适配功能
        AutoUtils.init(this, getDesignData("DESIGN_WIDTH"), getDesignData("DESIGN_HEIGHT"))
        //界面易工具初始化
        JieMainYiManager.init(this)
        //选择状态初始化
        SelectManager.init()
        //初始化Aria
        Aria.init(this).also {
            it.downloadConfig.run {
                isUseHeadRequest = true
                isUseBlock = true
                threadNum = 3
                connectTimeOut = 15000
                reTryNum = 3
                updateInterval = 1000
                isConvertSpeed = true
                buffSize = 1024 * 10
            }
        }
    }


    private fun getDesignData(key: String): Int {
        var design = 0
        try {
            val ai: ApplicationInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
            val bundle: Bundle = ai.metaData
            design = bundle.getInt(key, design)
        } catch (e: Exception) {
            LogUtils.e("getDesignMetaData: ${e.message}")
        }
        return design;
    }



}
